package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sy.simpleegg.R
import com.sy.simpleegg.bean.PayTypeDataYch
import com.sy.simpleegg.databinding.ItemPayTypeBinding
import com.sy.simpleegg.ext.onClick

class PayTypeYchAdapter(
    private var mContext: Context,
    private var payTypeData: MutableList<PayTypeDataYch>
) : RecyclerView.Adapter<PayTypeYchAdapter.ViewHolder>() {

    private var selectedId = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PayTypeYchAdapter.ViewHolder {
        val binding = ItemPayTypeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("NewApi", "SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: PayTypeYchAdapter.ViewHolder, position: Int) {
        val payType: PayTypeDataYch = payTypeData[position]

        if (payType.icon.isNotEmpty()) {
            holder.ivIcon.visibility = View.VISIBLE
            Glide.with(holder.itemView)
                .load(payType.icon)
                .fitCenter()
                .into(holder.ivIcon)
        }else{
            holder.ivIcon.visibility = View.GONE
        }

        holder.tvPayType.text = payType.name

        holder.btnPayType.background = if (payType.isSelected) mContext.resources.getDrawable(R.mipmap.dialog_btn_back_pressed) else mContext.resources.getDrawable(R.mipmap.dialog_btn_back_normal_bg)

        holder.btnPayType.onClick {
            setSelected(payType.code)
            mOnClickListener?.onClick(payType)
        }
    }

    override fun getItemCount(): Int {
        return payTypeData.size
    }

    private fun setSelected(code: Int){
        run setLastSelected@{
            payTypeData.forEach { item ->
                if (!item.isSelected){
                    return@forEach
                }
                if (item.code == code){
                    return
                }
                item.isSelected = false
                notifyItemChanged(item.code)
                return@setLastSelected
            }
        }

        run setCurrentSelected@{
            payTypeData.forEach { item ->
                if (item.code != code){
                    return@forEach
                }
                item.isSelected = true
                notifyItemChanged(item.code)
                return@setCurrentSelected
            }
        }
    }

    inner class ViewHolder(binding: ItemPayTypeBinding) : RecyclerView.ViewHolder(binding.root) {
        val btnPayType: RelativeLayout = binding.rlBtn
        val ivIcon: ImageView = binding.ivIcon
        val tvPayType: TextView = binding.tvPayType
    }

    interface OnItemClickListener {
        fun onClick(payTypaData: PayTypeDataYch?)
    }

    private var mOnClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnClickListener = onItemClickListener
    }
}